package jdk8;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author oldliu
 * @since 1.0
 */
public class StreamTest1 {
    public static void main(String[] args) {
        List<Bank> banks = Arrays.asList(
                new Bank("1", "liu1", "hefei", Math.random() * 1000),
                new Bank("2", "liu2", "beijing", Math.random() * 1000),
                new Bank("3", "liu3", "hefei", Math.random() * 1000),
                new Bank("9", "老刘", "anqing", Math.random() * 1000),
                new Bank("4", "老马", "hefei", Math.random() * 1000),
                new Bank("5", "小宋", "anqing", Math.random() * 1000),
                new Bank("6", "老吴", "hefei", Math.random() * 1000),
                new Bank("7", "小张", "huaibei", Math.random() * 1000));
        banks.forEach(System.out::println);
        System.out.println("1:扣5元的手续费");
        banks.forEach(x->{
            x.setMoney(x.getMoney()-5);
        });
        banks.forEach(System.out::println);
        System.out.println("2:找city是合肥的账户");
        List<Bank> hefei = banks.stream().filter(x -> x.getCity().equals("hefei")).collect(Collectors.toList());
        hefei.forEach(System.out::println);
        System.out.println("4一个城市留一个账户");
        List<Bank> h2 = banks.stream().distinct().collect(Collectors.toList());
        h2.forEach(System.out::println);
        System.out.println("跳3个取2个");
        List<Bank> c2 = banks.stream().skip(3).limit(2).collect(Collectors.toList());
        c2.forEach(System.out::println);
        System.out.println("<<获得储户的姓名");
        List<String> names = banks.stream().map(Bank::getName).collect(Collectors.toList());
        System.out.println(names);

        boolean b = banks.stream().anyMatch(x -> x.getMoney() < 100);
        System.out.println("还有小于100元的账户？"+b);
        System.out.println("所有账户的余额和");
        Double collect = banks.stream().collect(Collectors.summingDouble(x -> x.getMoney()));
        System.out.println(collect);
        Double avg = banks.stream().collect(Collectors.averagingDouble(x -> x.getMoney()));
        System.out.println(avg);
        Optional<Bank> max = banks.stream().max((x, y) -> (int) (x.getMoney() - y.getMoney()));
        System.out.println(max.isPresent()?max.get():null);
        System.out.println("按照城市统计账号的数量：");
        //hefei 3个，anqing 2g额，huaiwei 1个。。。
        Map<String, List<Bank>> cities = banks.stream().collect(Collectors.groupingBy(Bank::getCity, Collectors.toList()));

        cities.forEach((x,y)->{
            System.out.println(x+",,,"+y);
        });
    }
}
